package com.sky.controller.admin;


import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Api(tags = "后台-分类管理")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     */
    @PostMapping
    @ApiOperation(value = "新增分类")
    public Result addCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类");
        categoryService.saveCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 修改分类
     */
    @PutMapping
    @ApiOperation(value = "修改分类")
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改分类");
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 根据id删除分类
     */
    @DeleteMapping
    @ApiOperation(value = "根据id删除分类")
    public Result deleteCategory(Long id) {
        log.info("根据id删除分类");
        categoryService.deleteCategory(id);
        return Result.success();
    }


    /**
     * 分页查询分类列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询分类")
    public Result<PageResult> pageCategory(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分页查询分类");
        PageResult pageResult = categoryService.pageCategory(categoryPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 根据类型查询分类
     */
    @GetMapping("/list")
    @ApiOperation(value = "根据类型查询分类")
    public Result<List<Category>> getCategoryByType(Integer type) {
        log.info("根据类型查询分类");
        List<Category> category = categoryService.getCategoryByType(type);
        return Result.success(category);
    }

    /**
     * 启用禁用分类
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "启用禁用分类")
    public Result updateCategoryStatus(@PathVariable("status") Integer status, Long id) {
        log.info("启用/禁用分类：{},{}", status, id);
        categoryService.updateCategoryStatus(status, id);
        return Result.success();
    }
}
